{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "*****Movie*****\n",
      "Title: Enemy Behind\n",
      "Type: War, Thriller\n",
      "Format: DVD\n",
      "Year: 2003\n",
      "Rating: PG\n",
      "Stars: 10\n",
      "Description: Talk about a US-Japan war\n",
      "*****Movie*****\n",
      "Title: Transformers\n",
      "Type: Anime, Science Fiction\n",
      "Format: DVD\n",
      "Year: 1989\n",
      "Rating: R\n",
      "Stars: 8\n",
      "Description: A schientific fiction\n",
      "*****Movie*****\n",
      "Title: Trigun\n",
      "Type: Anime, Action\n",
      "Format: DVD\n",
      "Rating: PG\n",
      "Stars: 10\n",
      "Description: Vash the Stampede!\n",
      "*****Movie*****\n",
      "Title: Ishtar\n",
      "Type: Comedy\n",
      "Format: VHS\n",
      "Rating: PG\n",
      "Stars: 2\n",
      "Description: Viewable boredom\n"
     ]
    }
   ],
   "source": [
    "#!/usr/bin/python\n",
    "# -*- coding: UTF-8 -*-\n",
    " \n",
    "import xml.sax\n",
    " \n",
    "class MovieHandler( xml.sax.ContentHandler ):\n",
    "   def __init__(self):\n",
    "      self.CurrentData = \"\"\n",
    "      self.type = \"\"\n",
    "      self.format = \"\"\n",
    "      self.year = \"\"\n",
    "      self.rating = \"\"\n",
    "      self.stars = \"\"\n",
    "      self.description = \"\"\n",
    " \n",
    "   # 元素开始事件处理\n",
    "   def startElement(self, tag, attributes):\n",
    "      self.CurrentData = tag\n",
    "      if tag == \"movie\":\n",
    "         print \"*****Movie*****\"\n",
    "         title = attributes[\"title\"]\n",
    "         print \"Title:\", title\n",
    " \n",
    "   # 元素结束事件处理\n",
    "   def endElement(self, tag):\n",
    "      if self.CurrentData == \"type\":\n",
    "         print \"Type:\", self.type\n",
    "      elif self.CurrentData == \"format\":\n",
    "         print \"Format:\", self.format\n",
    "      elif self.CurrentData == \"year\":\n",
    "         print \"Year:\", self.year\n",
    "      elif self.CurrentData == \"rating\":\n",
    "         print \"Rating:\", self.rating\n",
    "      elif self.CurrentData == \"stars\":\n",
    "         print \"Stars:\", self.stars\n",
    "      elif self.CurrentData == \"description\":\n",
    "         print \"Description:\", self.description\n",
    "      self.CurrentData = \"\"\n",
    " \n",
    "   # 内容事件处理\n",
    "   def characters(self, content):\n",
    "      if self.CurrentData == \"type\":\n",
    "         self.type = content\n",
    "      elif self.CurrentData == \"format\":\n",
    "         self.format = content\n",
    "      elif self.CurrentData == \"year\":\n",
    "         self.year = content\n",
    "      elif self.CurrentData == \"rating\":\n",
    "         self.rating = content\n",
    "      elif self.CurrentData == \"stars\":\n",
    "         self.stars = content\n",
    "      elif self.CurrentData == \"description\":\n",
    "         self.description = content\n",
    "  \n",
    "if ( __name__ == \"__main__\"):\n",
    "   \n",
    "   # 创建一个 XMLReader\n",
    "   parser = xml.sax.make_parser()\n",
    "   # turn off namepsaces\n",
    "   parser.setFeature(xml.sax.handler.feature_namespaces, 0)\n",
    " \n",
    "   # 重写 ContextHandler\n",
    "   Handler = MovieHandler()\n",
    "   parser.setContentHandler( Handler )\n",
    "   \n",
    "   parser.parse(\"movies.xml\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 2",
   "language": "python",
   "name": "python2"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.15"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
